﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Windows.Forms;

namespace PowerApp
{
    public partial class OpenComForm2 : Form
    {
        public OpenComForm2()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            // defines from WinBase.h
            //#define CBR_110             110
            //#define CBR_300             300
            //#define CBR_600             600
            //#define CBR_1200            1200
            //#define CBR_2400            2400
            //#define CBR_4800            4800
            //#define CBR_9600            9600
            //#define CBR_14400           14400
            //#define CBR_19200           19200
            //#define CBR_38400           38400
            //#define CBR_56000           56000
            //#define CBR_57600           57600
            //#define CBR_115200          115200
            //#define CBR_128000          128000
            //#define CBR_256000          256000

            //#define NOPARITY            0
            //#define ODDPARITY           1
            //#define EVENPARITY          2
            //#define MARKPARITY          3
            //#define SPACEPARITY         4

            //#define ONESTOPBIT          0
            //#define ONE5STOPBITS        1
            //#define TWOSTOPBITS         2

            bpscomboBox1.DataSource = new List<string>
            {
                "110",
                "300",
                "600",
                "1200",
                "2400",
                "4800",
                "7200",
                "9600",
                "14400",
                "19200",
                "38400",
                "57600",
                "115200",
                "128000",
            };
            bpscomboBox1.SelectedItem = "9600";
            bpscomboBox1.KeyPress += delegate (object obj, KeyPressEventArgs ev)
            {
                if (char.IsDigit(ev.KeyChar) || char.IsControl(ev.KeyChar))
                { }
                else
                {
                    ev.Handled = true;
                    System.Media.SystemSounds.Beep.Play();
                }
            };

            databitscomboBox2.DataSource = new List<int> { 4, 5, 6, 7, 8 };
            databitscomboBox2.SelectedItem = 8;

            paritycomboBox1.DataSource = new List<object>
            {
                new {Value=2, Text="偶" },
                new {Value=1, Text="奇" },
                new {Value=0, Text="无" },
                new {Value=3, Text="标志" },
                new {Value=4, Text="空格" },
            };
            paritycomboBox1.ValueMember = "Value";
            paritycomboBox1.DisplayMember = "Text";
            paritycomboBox1.SelectedValue = 0;

            stopbitscomboBox2.DataSource = new List<object>
            {
                new {Value=0, Text="1" },
                new {Value=1, Text="1.5" },
                new {Value=2, Text="2" },
            };
            stopbitscomboBox2.ValueMember = "Value";
            stopbitscomboBox2.DisplayMember = "Text";
            stopbitscomboBox2.SelectedValue = 0;

            base.OnLoad(e);
        }

        protected override void OnShown(EventArgs e)
        {
            refreshbutton3_Click(null, null);

            base.OnShown(e);
        }

        public int Num { get { return this.num; } }
        public int BPS { get { return this.bps; } }
        public int DataBits { get { return this.databits; } }
        public int Parity { get { return this.parity; } }
        public int StopBits { get { return this.stopbits; } }

        int num = 0;
        int bps = 0;
        int databits = 0;
        int parity = 0;
        int stopbits = 0;
        private void okbutton1_Click(object sender, EventArgs e)
        {
            try
            {
                this.num = (int)listBox1.SelectedValue;

                this.bps = int.Parse(bpscomboBox1.Text);

                this.databits = (int)databitscomboBox2.SelectedItem;

                this.parity = (int)paritycomboBox1.SelectedValue;

                this.stopbits = (int)stopbitscomboBox2.SelectedValue;

                this.DialogResult = DialogResult.OK;
            }
            catch
            {
                MessageBox.Show("请输入合理的数值", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            okbutton1.Enabled = (listBox1.SelectedIndex >= 0);
        }

        private void refreshbutton3_Click(object sender, EventArgs e)
        {
            List<object> list = new List<object>();

            foreach (string port in SerialPort.GetPortNames())
            {
                try
                {
                    int num = int.Parse(port.Substring(3));

                    list.Add(new { Value = num, Text = port });
                }
                catch { }
            }

            listBox1.DataSource = list;
            listBox1.ValueMember = "Value";
            listBox1.DisplayMember = "Text";
        }
    }
}
